package view.controller;

import common.Group;
import common.Message;
import common.NowUser;
import common.User;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import server.ClientConnServer;
import utils.MD5;
import view.GroupInfoOwner;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.BrokenBarrierException;

public class ChangeGroupInfoController implements Initializable {
    public Label grid;
    public Label grname;
    public ImageView img;
    public ImageView grchangeimg;
    public TextField groupname;
    public TextArea groupintroduction;
    public Button changetrue;
    public File localFile;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        groupname.setText(NowUser.group.getGroupname());
        grname.setText(NowUser.group.getGroupname());
        grid.setText(NowUser.group.getGroupid());
        groupintroduction.setText(NowUser.group.getGroupsign());
        if(NowUser.group.getGroupheadimage()!=null && !NowUser.group.getGroupheadimage().equals("")&&!NowUser.group.getGroupheadimage().equals("null")){
            // System.out.println(user.getPhoto());
            Image image=new Image("file:" + NowUser.group.getGroupheadimage());
            img.setImage(image);
        }else{
            Image image=new Image("view\\photo\\群头像.jpg");
            img.setImage(image);
        }
        if(NowUser.group.getGroupheadimage()!=null && !NowUser.group.getGroupheadimage().equals("")&&!NowUser.group.getGroupheadimage().equals("null")){
            // System.out.println(user.getPhoto());
            Image image=new Image("file:" + NowUser.group.getGroupheadimage());
            grchangeimg.setImage(image);
        }else{
            Image image=new Image("view\\photo\\群头像.jpg");
            grchangeimg.setImage(image);
        }
    }

    public void onchangetrue(ActionEvent actionEvent) throws IOException, BrokenBarrierException,InterruptedException {
        Group group = new Group();
        MD5 md5 = new MD5();
        if(groupname.getText().equals("")){
            JOptionPane.showMessageDialog(null, "昵称不能为空", "提示", JOptionPane.ERROR_MESSAGE);
        }else {
            if(groupname.getText()!=null){
                group.setGroupname(groupname.getText());
            }
            group.setGroupsign(groupintroduction.getText());
            Message cgmsg = new Message();
            group.setGroupid(grid.getText());
            cgmsg.setGroup(group);
            //System.out.println("sss"+cgmsg.getUser().getGender());
            cgmsg.setHeadimage(localFile);
            cgmsg.setType("changeGroupInformation");
            NowUser.changegroupinformation=false;
            new ClientConnServer().sendinfo(cgmsg);
            NowUser.cyclicBarrier.reset();
            NowUser.cyclicBarrier.await();
            if (NowUser.changegroupinformation) {
                Message msg2=new Message();
                msg2.setType("getgroup");
                msg2.setGroup(group);
                new ClientConnServer().sendinfo(msg2);
                NowUser.cyclicBarrier.reset();
                NowUser.cyclicBarrier.await();
                JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.PLAIN_MESSAGE);
                Stage stage = (Stage) changetrue.getScene().getWindow();
                GroupInfoOwner rj = new GroupInfoOwner();
                rj.start(stage);
            } else {
                JOptionPane.showMessageDialog(null, "修改失败", "提示", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    //上传头像
    public void upimg(MouseEvent mouseEvent) {
        User user = new User();
        FileChooser fileChooser = new FileChooser(); //文件选择窗口
        fileChooser.setTitle("Open Resource File");  //窗口名
        fileChooser.setInitialDirectory(new File("C:\\Users")); //设置默认打开的文件路径
        File file = fileChooser.showOpenDialog(new Stage());//显示屏幕选择窗口
        // String filePath = file.getCanonicalPath()+"/fileSource"; //指定路径
        String str =null;
        if(file != null) {
            str = String.valueOf(file.getAbsolutePath());  // 方法返回选择文件的相对路径
            String url = "file:" + str;
            NowUser.user.setHeadimage(url);
            grchangeimg.setImage(new Image(url,100,100,true,true,true));
        }
        //读取本地文件
        localFile = new File(str);
    }
}
